import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Pie
import urllib,urllib.request

def data_hn():
    """ 制作华南片区的广州地区的天气数据"""
    headers = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
    req = urllib.request.Request(url="http://www.weather.com.cn/textFC/hn.shtml", headers=headers)
    df0 = urllib.request.urlopen(req).read()
    df = pd.read_html(df0)[1] #取出华南广州片区的数据
    df_hn = df.iloc[:, 1:8]   #去掉多余的标题
    title = df_hn.iloc[1].tolist() #将数据第一行的标题以列表格式取出
    td_content = []
    for i in range(2,22):
        td_content.append(df_hn.iloc[i:i + 1].T[i].tolist()) #将每行数据便利到列表里
    return title,td_content
# print(data_hn())

def hn_visual():
    """制作柱状图可视化图表"""
    #先将所需数据运行出来（同data_hn函数部分数据）
    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
    req = urllib.request.Request(url="http://www.weather.com.cn/textFC/hn.shtml", headers=headers)
    df0 = urllib.request.urlopen(req).read()
    df = pd.read_html(df0)[1]
    df_hn = df.iloc[:, 1:8]
    title = df_hn.iloc[1].tolist()
    td_content = []
    for i in range(2,22):
        td_content.append(df_hn.iloc[i:i + 1].T[i].tolist())

    #取出城市列表
    columns1 = df_hn.iloc[:, 0].tolist()
    del columns1[:2]
    columns = columns1

    #取出最高气温列表
    data1 = df_hn.iloc[:,3].tolist()
    del data1[:2]
    try:
        data1 = list(map(int, data1)) #将字符串转化为数字
    except ValueError as e:
        pass
    else:
        high = data1

    #取出最低气温列表
    data2 = df_hn.iloc[:,6].tolist()
    del data2[:2]
    try:
        data2 = list(map(int, data2)) #将字符串转化为数字
    except ValueError as e:
        pass
    else:
        low = data2
    #柱状图函数

    bar = (

        Bar()
            .add_xaxis(columns)
            .add_yaxis("最高气温", high)
            .add_yaxis("最低气温", low)
            .set_global_opts(title_opts=opts.TitleOpts(title="广东各城市今日最高与最低气温柱状图"))
    )
    return bar

def hn_pie_day():
    """制作白天天气现象的饼状图"""
    # 先将每日数据运行出来
    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
    req = urllib.request.Request(url="http://www.weather.com.cn/textFC/hn.shtml", headers=headers)
    df0 = urllib.request.urlopen(req).read()
    df = pd.read_html(df0)[1]
    df_hn = df.iloc[:, 1:8]
    title = df_hn.iloc[1].tolist()
    td_content = []
    for i in range(2, 22):
        td_content.append(df_hn.iloc[i:i + 1].T[i].tolist())
    #取出天气现象列表
    columns1 = df_hn.iloc[:, 1].tolist()
    del columns1[:2]
    #将重复元素去掉
    list_columns1 = list({}.fromkeys(columns1).keys())
    #将对应的天气现象数量返回出来
    result1 = []
    for a in list_columns1:
        result1.append(columns1.count(a))
    from pyecharts import options as opts
    from pyecharts.charts import Pie
    #饼图函数
    c1 = (
        Pie()
            .add("", [list(z) for z in zip(list_columns1, result1)])
            .set_colors(["#FFCCCC", "#FFCC99", "#FFFFCC", "#99CCFF", "#66CCCC", "#CCCC66"])
            .set_global_opts(title_opts=opts.TitleOpts(title="广东各城市白天天气现象集合"))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))

    )
    return c1

def hn_pie_night():
    """制作夜间天气现象的饼状图"""
    # 先将每日数据运行出来
    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
    req = urllib.request.Request(url="http://www.weather.com.cn/textFC/hn.shtml", headers=headers)
    df0 = urllib.request.urlopen(req).read()
    df = pd.read_html(df0)[1]
    df_hn = df.iloc[:, 1:8]
    title = df_hn.iloc[1].tolist()
    td_content = []
    for i in range(2, 22):
        td_content.append(df_hn.iloc[i:i + 1].T[i].tolist())
    #取出天气现象列表
    columns_night = df_hn.iloc[:, 4].tolist()
    del columns_night[:2]
    #将重复元素去掉
    list_columns2 = list({}.fromkeys(columns_night).keys())
    #将对应的天气现象数量返回出来
    result2 = []
    for a in list_columns2:
        result2.append(columns_night.count(a))
    #饼图函数
    c2 = (
        Pie()
            .add("", [list(z) for z in zip(list_columns2, result2)])
            .set_colors(["#000066", "#6666CC", "#9999CC", "#666699", "#666699", "#333399"])
            .set_global_opts(title_opts=opts.TitleOpts(title="广东各城市夜间天气现象集合"))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))

    )
    return c2

